package hk.com.fgoproduction.getdroplets.Lib.FileMan

import android.app.Activity
import android.util.SparseBooleanArray
import android.view.View
import android.widget.AdapterView
import android.widget.ListView
import hk.com.fgoproduction.getdroplets.Lib.TreeList.TreeList
import hk.com.fgoproduction.getdroplets.R
import kotlinx.android.synthetic.main.file_man_view.*
import java.util.*

abstract class FileManActFrame() : Activity(), AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    abstract val showLog: Boolean

    protected var tree = TreeList()
    protected var view: ListView? = null
    protected val showList = ArrayList<String>()
    protected lateinit var modAdapter: ModAdapter
    protected var isChecking = false

    abstract val ROOT: String
    abstract var currentPath: String

    abstract fun renewList(path: String)
    abstract fun updateBundle(path: String)
    abstract protected fun changeDir(position: Int)

    protected fun restartActivity() {
        finish()
        startActivity(intent)
    }

    protected fun setAdapter(value: ViewLayout, checkList: ShortArray? = null) {
        val layout: Int
        val choiceMode: Int

        when (value) {

            ViewLayout.LIST_BASIC -> {
                layout = R.layout.file_man_view_item
                choiceMode = ListView.CHOICE_MODE_SINGLE
                isChecking = false
                select_all_btn.visibility = View.GONE
                select_all_btn.isClickable = false
            }

            ViewLayout.LIST_CHECK -> {
                layout = R.layout.file_man_view_item_w_check
                choiceMode = ListView.CHOICE_MODE_MULTIPLE
                isChecking = true
                select_all_btn.visibility = View.VISIBLE
                select_all_btn.isClickable = true
            }

            else -> {
                throw Exception("Check $this")
            }
        }

        modAdapter = ModAdapter(showList, this, layout, isChecking)
        if (checkList !== null) initCheckView(checkList)

        view?.adapter = modAdapter
        view?.choiceMode = choiceMode

        view?.onItemLongClickListener = if (value == ViewLayout.LIST_BASIC) this else null
    }

    protected fun initCheckView(checkList: ShortArray) {
        val tempCheckList = SparseBooleanArray()
        for (i in 0..checkList.size - 1) {
            tempCheckList.append(i, checkList[i] == 1.toShort())
        }
        modAdapter.setIsSelected(tempCheckList)
    }

    override fun onBackPressed() {
        if (!isChecking) {
            if (currentPath == ROOT)
                super.onBackPressed()
            else
                renewList(tree.get(currentPath)?.parentPath as String)
        } else {
            setAdapter(ViewLayout.LIST_BASIC)
            renewList(currentPath)
        }
    }

    override fun onDestroy() {
        tree.clear()
        showList.clear()
        super.onDestroy()
    }

    protected fun checkItem(view: View, position: Int) {
        val holder = view.tag as ModAdapter.ViewHolder
        if (holder.textView.text != "../" && holder.textView.text != "/") {
            holder.checkBox?.toggle()
            modAdapter.notIsSelected(position)
        }
    }

    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {
        setAdapter(ViewLayout.LIST_CHECK)
        renewList(currentPath)

        return true
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        if (isChecking) {
            checkItem(view!!, position)
        } else {
            changeDir(position)
        }
        //super.onListItemClick(l, v, position, id)
    }

    enum class ViewLayout() {
        LIST_BASIC,
        LIST_CHECK
    }

}